package com.zhuzhe.blog.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.system.entity.SysRole;
import com.zhuzhe.blog.system.entity.SysRoleMenu;
import com.zhuzhe.blog.system.entity.SysUserRole;
import com.zhuzhe.blog.system.mapper.SysRoleMapper;
import com.zhuzhe.blog.system.mapper.SysRoleMenuMapper;
import com.zhuzhe.blog.system.mapper.SysUserRoleMapper;
import com.zhuzhe.blog.system.requestDto.SysRolePageDto;
import com.zhuzhe.blog.system.responseVo.SysRoleListVo;
import com.zhuzhe.blog.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author u53
 * @since 2020-10-13
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;


    /**
     * 分页查询角色列表
     *
     * @param sysRolePageDto
     * @return com.zhuzhe.blog.base.ResponseData<com.baomidou.mybatisplus.core.metadata.IPage < com.zhuzhe.blog.system.responseVo.SysRoleListVo>>
     * @throws
     * @author whh
     * @date 2020/10/17 15:24
     */
    @Override
    public ResponseData<IPage<SysRoleListVo>> queryPage(SysRolePageDto sysRolePageDto) {

        //条件查询
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysRolePageDto.getName())) {
            queryWrapper.like("name", sysRolePageDto.getName());
        }
        IPage<SysRole> sysRoleIPage = baseMapper.selectPage(sysRolePageDto.getPage(), queryWrapper);
        //返回封装
        IPage<SysRoleListVo> sysRoleListVoIPage = new Page<>();
        sysRoleListVoIPage.setRecords(Convert.convertList(sysRoleIPage.getRecords(), SysRoleListVo.class));
        return JsonData.success(sysRoleListVoIPage);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return com.zhuzhe.blog.base.ResponseData<java.lang.String>
     * @throws
     * @author whh
     * @date 2020/10/17 15:24
     */
    @Override
    @Transactional
    public ResponseData<String> deleteById(String id) {

        if (StringUtils.isEmpty(id)) {
            return JsonData.fail("id不能为空！！！");
        }

        SysRole sysRole = baseMapper.selectById(id);
        if (null == sysRole) {
            return JsonData.fail("角色信息不存在");

        }
        //删除角色
        baseMapper.deleteById(id);
        QueryWrapper<SysRoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        //删除角色和菜单关联
        roleMenuQueryWrapper.eq("role_id", id);
        roleMenuMapper.delete(roleMenuQueryWrapper);
        //删除用户和角色关联
        QueryWrapper<SysUserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("role_id", id);
        userRoleMapper.delete(userRoleQueryWrapper);
        return JsonData.success(id);
    }

    @Override
    public ResponseData<List<String>> findMenuIdsById(String id) {
        List<String> menuIdsById = baseMapper.findMenuIdsById(id);
        return JsonData.success(menuIdsById);
    }

    @Override
    public ResponseData<Integer> saveRoleMenu(String id, List<String> menuIds) {

        //删除角色菜单关联表数据
        QueryWrapper<SysRoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.eq("role_id", id);
        roleMenuMapper.delete(roleMenuQueryWrapper);

        //讲新增信息保留到关联表中
        if (CollectionUtils.isNotEmpty(menuIds)) {
            Integer result = baseMapper.saveRoleMenu(id, menuIds);
            return JsonData.success(id);
        }
        return JsonData.fail("操作失败！！！");
    }
}
